﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Newtonsoft.Json;
using log4net;
using Common;
using AutoFacUtils;
using CashFlow.Db.Entity;
using CashFlow.ShopManage.Def;
using CashFlow.ShopManage.Common;
using CashFlow.ShopManage.Filters;
using CashFlow.ShopManage.Models.Order;
using CashFlow.ShopManage.Models.Order.Result;
using CashFlow.ShopManage.Service;
using CashFlow.ShopManage.Validator.Order;
using CashFlow.ShopManage.Validator;
using CashFlow.ShopManage.Models;

namespace CashFlow.ShopManage.Controllers.Ordinary
{
    public class OrderController : Controller
    {
        private static ILog _logger = LogManager.GetLogger(typeof(OrderController));
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IOrderService GetOrderService() { return AutoFacHelper.Get<IOrderService>(); }

        // GET: Order
        public ActionResult Index()
        {
            return View();
        }

        [ValidatorFilter(typeof(QueryOrderModelValidator))]
        public ActionResult List(QueryOrderModel model)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var orderService = GetOrderService();
            var ls = orderService.QueryShopAllUsersOrder(
                        ui.ShopManagerUserId
                        , model.OrderNo
                        , model.Date
                        , model.State
                        , model.Buyer
                        , model.Seller
                     );
            var ls2 = ls.Select(x => Convert(x)).ToList();
            var sum = ls2.Sum(x => x.Amount);
            return MyCommonTool.CreateLayUITableResult(
                        true
                        , ""
                        , ls2
                        , ls2.Count
                   );
        }

        public ActionResult CreateOrder()
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var userService = GetUserService();
            var ls = userService.QueryShopAllUsersByShopManagerUserId(
                        ui.ShopManagerUserId
                        , 1
                     );
            var ls2 = ls.Select(x => new
            {
                Id = x.Id,
                Name = x.Name,
                Phone = x.Phone
            }).ToList();
            ViewBag.UserListJson = JsonConvert.SerializeObject(ls2);

            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(CreateOrderModelValidator))]
        public ActionResult CreateOrder(CreateOrderModel model)
        {
            var service = GetOrderService();
            var result = service.AddOrder(
                            model.BuyerUserId
                            , model.SellerUserId
                            , model.Amount
                         );
            return MyCommonTool.CreateOperationResult(
                        result == ECreateOrderResult.Success
                        , EnumTool.GetDescription<ECreateOrderResult, int>(result)
                        , null
                   );
        }

        public ActionResult PremiumOrder()
        {
            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(DateValidator))]
        public ActionResult QueryPremiumOrder(DateModel model)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var userService = GetUserService();
            var orderService = GetOrderService();
            var ls = orderService.QueryShopAllUsersOrder2(
                        ui.ShopManagerUserId
                        , model.Date
                     );
            var ls2 = ls.Select(x => new
            {
                BuyUserId = x.OrderInfo.BuyerUserId,
                BuyerName = x.UserInfo.Name,
                Amount = x.OrderInfo.Amount * (1M + Consts.INCREASE_RATE)
            }).OrderBy(x => x.Amount).ToList();

            return MyCommonTool.CreateOperationResult(true, "", ls2);
        }

        public ActionResult ReleaseOrder()
        {
            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(DateValidator))]
        public ActionResult ReleaseOrder(DateModel model)
        {
            var orderService = GetOrderService();
            bool b = orderService.ReleaseOrder(model.Date);
            return MyCommonTool.CreateOperationResult(b, "", "");
        }

        [HttpPost]
        [ValidatorFilter(typeof(UpdateOrderModelValidator))]
        public ActionResult UpdateOrder(UpdateOrderModel model)
        {
            string date = MyCommonTool.GetOrderDate();
            var service = GetOrderService();
            var result = service.UpdateOrder(
                            model.OrderId
                            , model.BuyerUserId
                            , model.SellerUserId
                            , model.Amount
                            , date
                         );
            return MyCommonTool.CreateOperationResult(
                        result == EUpdateOrderResult.Success
                        , EnumTool.GetDescription<EUpdateOrderResult, int>(result)
                        , ""
                   );
        }

        private QueryOrderResultModel Convert(OrderBuySellInfo model)
        {
            return new QueryOrderResultModel
            {
                Id = model.Order.Id,
                OrderNo = model.Order.OrderNo,
                ProductName = model.Order.Name,
                Buyer = model.Buyer.Name,
                Seller = model.Seller.Name,
                Amount = model.Order.Amount,
                Time = model.Order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                State = model.Order.State,
                Visible = model.Order.Visible,
                PaymentVoucherImagePath = model.Order.PaymentVoucherImagePath,
                BuyerUserId = model.Order.BuyerUserId,
                SellerUserId = model.Order.SellerUserId
            };
        }
    }
}